package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.activity.BonusOptimizationActivity;
import com.itboye.lottery.bean.ZqBetListItem;
import com.itboye.lottery.bean.ZqMatchDataCheck;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class BonusOptimizationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<ZqMatchDataCheck.ListEntity> classEntityArrayList = new ArrayList<>();
    Activity activity;

    public BonusOptimizationAdapter(Activity activity, ArrayList<ZqMatchDataCheck.ListEntity> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
        this.activity = activity;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(BonusOptimizationAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_bonus_optimization, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ZqMatchDataCheck.ListEntity zqBetListItem = classEntityArrayList.get(position);
        ViewHolder viewHolder = ((ViewHolder) holder);
        if (zqBetListItem.getMatch_list().size() > 0) {
            viewHolder.item_bonusopt_tv_team.setText(zqBetListItem.getMatch_list().get(0).gethTeam());
        }
        int multiple = zqBetListItem.getMultiple();
        viewHolder.et_multiple.setText(multiple + "");
        viewHolder.et_multiple.setTag(position);
        viewHolder.et_multiple.setOnClickListener((BonusOptimizationActivity)activity);
//        viewHolder.et_multiple.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                int tempMultiple;
//                if (TextUtils.isEmpty(viewHolder.et_multiple.getText().toString())) {
//                    tempMultiple = 0;
//                } else {
//                    tempMultiple = Integer.parseInt(viewHolder.et_multiple.getText().toString());
//                }
//                int tempPosition = (int) viewHolder.et_multiple.getTag();
//                classEntityArrayList.get(tempPosition).setMultiple(tempMultiple);
//                notifyDataSetChanged();
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
        viewHolder.tv_single_bonus.setText(String.format("%.2f", zqBetListItem.getSingleBonus()));//单倍金额
        viewHolder.tv_bonus.setText(String.format("%.2f", zqBetListItem.getSingleBonus() * multiple));//奖金


        viewHolder.item_bonusopt_tv_team.setTag(position);
        viewHolder.item_bonusopt_tv_team.setOnClickListener((BonusOptimizationActivity) activity);
        viewHolder.tv_jian.setTag(position);
        viewHolder.tv_jian.setOnClickListener((BonusOptimizationActivity) activity);
        viewHolder.tv_jia.setTag(position);
        viewHolder.tv_jia.setOnClickListener((BonusOptimizationActivity) activity);
        viewHolder.ll_detail_container.removeAllViews();
        viewHolder.include_headerview.setBackgroundColor(Color.parseColor("#4D4D4D"));
        for (ZqBetListItem betListItem : zqBetListItem.getMatch_list()) {
            View view = View.inflate(context, R.layout.item_bonus_optimization_childview, null);
            TextView tv_one = view.findViewById(R.id.tv_one);
            TextView tv_two = view.findViewById(R.id.tv_two);
            TextView tv_three = view.findViewById(R.id.tv_three);
            TextView tv_four = view.findViewById(R.id.tv_four);
            tv_one.setText(betListItem.getNum());
            tv_two.setText(betListItem.gethTeam());
            tv_three.setText(betListItem.getaTeam());
            tv_four.setText(betListItem.getBetContent());
            viewHolder.ll_detail_container.addView(view);
        }
        if (zqBetListItem != null) {
            if (zqBetListItem.isVisible()) {
                viewHolder.include_headerview.setVisibility(View.VISIBLE);
                viewHolder.ll_detail_container.setVisibility(View.VISIBLE);
                viewHolder.item_bonusopt_tv_team.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.mipmap.arrow_up);
            } else {
                viewHolder.ll_detail_container.setVisibility(View.GONE);
                viewHolder.include_headerview.setVisibility(View.GONE);
                viewHolder.item_bonusopt_tv_team.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.mipmap.arrow_down);
            }
        }

    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView title;
        public TextView content, item_bonusopt_tv_team, tv_single_bonus, tv_bonus;
        LinearLayout ll_detail_container;
        TextView tv_jian, tv_jia;
        TextView et_multiple;
        View include_headerview;

        public ViewHolder(View itemView) {
            super(itemView);
            ll_detail_container = itemView.findViewById(R.id.ll_detail_container);
            tv_jian = itemView.findViewById(R.id.tv_jian);
            tv_jia = itemView.findViewById(R.id.tv_jia);
            et_multiple = itemView.findViewById(R.id.et_multiple);
            item_bonusopt_tv_team = itemView.findViewById(R.id.item_bonusopt_tv_team);
            tv_single_bonus = itemView.findViewById(R.id.tv_single_bonus);
            tv_bonus = itemView.findViewById(R.id.tv_bonus);
            include_headerview = itemView.findViewById(R.id.include_headerview);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
